package org.luckyjourney.service.regularusers.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.checkerframework.checker.units.qual.A;
import org.luckyjourney.config.QiNiuConfig;
import org.luckyjourney.constant.AuditStatus;
import org.luckyjourney.entity.ApiConstant;
import org.luckyjourney.entity.File;
import org.luckyjourney.entity.RegularRole;
import org.luckyjourney.entity.plan.Plan;
import org.luckyjourney.entity.plan.dto.PlanDTO;
import org.luckyjourney.entity.plan.vo.PlanVO;
import org.luckyjourney.entity.plan.vo.PlanVOS;
import org.luckyjourney.entity.regularusers.RegularUsers;
import org.luckyjourney.entity.response.AuditResponse;
import org.luckyjourney.entity.user.*;
import org.luckyjourney.entity.user.dto.UserDTO;
import org.luckyjourney.entity.video.Type;
import org.luckyjourney.entity.video.Video;
import org.luckyjourney.entity.vo.*;
import org.luckyjourney.exception.BaseException;
import org.luckyjourney.holder.UserHolder;
import org.luckyjourney.mapper.RegularRoleMapper;
import org.luckyjourney.mapper.booking.BookingMapper;
import org.luckyjourney.mapper.plan.ConsultantsPlanMapper;
import org.luckyjourney.mapper.regularusers.RegularUsersMapper;
import org.luckyjourney.mapper.user.PersonalLabelsMapper;
import org.luckyjourney.mapper.user.TrainingMapper;
import org.luckyjourney.mapper.user.UserRoleMapper;
import org.luckyjourney.mapper.video.VideoMapper;
import org.luckyjourney.service.FileService;
import org.luckyjourney.service.InterestPushService;
import org.luckyjourney.service.audit.ImageAuditService;
import org.luckyjourney.service.audit.TextAuditService;
import org.luckyjourney.service.regularusers.RegularUsersService;
import org.luckyjourney.service.user.FavoritesService;
import org.luckyjourney.service.user.FollowService;
import org.luckyjourney.service.user.UserService;
import org.luckyjourney.service.user.UserSubscribeService;
import org.luckyjourney.service.video.TypeService;
import org.luckyjourney.service.video.VideoService;
import org.luckyjourney.util.BaseContext;
import org.luckyjourney.util.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class RegularUsersServiceImpl extends ServiceImpl<RegularUsersMapper, RegularUsers> implements RegularUsersService {
    //咨询师表
    @Autowired
    RegularUsersMapper regularUsersMapper;

    @Autowired
    private FollowService followService;

    @Autowired
    private FileService fileService;
    @Autowired
    UserService userService;

    @Autowired
    PersonalLabelsMapper personalLabelsMapper;
    @Autowired
    TrainingMapper trainingMapper;

    @Autowired
    BookingMapper bookingMapper;

    @Autowired
    ConsultantsPlanMapper consultantsPlanMapper;

    @Override
    public List<RegularUsers> list(Collection<Long> userIds) {

        return list(new LambdaQueryWrapper<RegularUsers>().in(RegularUsers::getId, userIds)
                .select(RegularUsers::getId, RegularUsers::getNickName, RegularUsers::getSex, RegularUsers::getAvatar, RegularUsers::getDescription));

    }

    @Override
    public UserVO getconsultants(Long userId) {
        //查询咨询师的详情
        final RegularUsers user = getById(userId);
        if (ObjectUtils.isEmpty(user)) {
            return new UserVO();
        }
        final UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        // 查出关注数量
        final long followCount = followService.getFollowCount(userId);
        //获取点赞量
        long startCount = regularUsersMapper.getStartCount(userId);
        // 查出咨询师粉丝数量
        final long fansCount = followService.getFansCountPs(userId);
        if (ObjectUtil.isNotEmpty(user.getAvatar())) {
            File file = fileService.getById(user.getAvatar());
            String a = QiNiuConfig.CNAME + "/" + file.getFileKey();
            userVO.setA(a);
            userVO.setAvatar(file.getId());
        }
        Integer moduleId = user.getModuleId();
        userVO.setStartCount(startCount);
        userVO.setFollow(followCount);
        userVO.setFans(fansCount);
        userVO.setModuleId(moduleId);
        //获取当前登录人的信息查看关注列表是否有该咨询师
        Long currentId = BaseContext.getCurrentId();
        UserVO userVO1 = userService.getInfo(currentId);
        Follow follow2 = new Follow();
        follow2.setUserId(currentId);
        follow2.setMyselfId(userVO1.getModuleId());
        follow2.setFollowId(userId);
        // 获取自己的关注列表
        List<Follow> follow = followService.selectFollow(follow2);
        if (ObjectUtil.isNotEmpty(follow)) {
            for (Follow follow1 : follow) {
                Long followId = follow1.getFollowId();
                Integer moduleId1 = follow1.getModuleId();
                if (user.getId().equals(followId) && moduleId.equals(moduleId1)) {
                    userVO.setStatus(true);
                } else {
                    userVO.setStatus(false);
                }

            }

        } else {
            userVO.setStatus(false);
        }

        if (ObjectUtil.isNotEmpty(user.getCover())) {
            File file = fileService.getById(user.getCover());
            String c = QiNiuConfig.CNAME + "/" + file.getFileKey();
            userVO.setC(c);
            userVO.setCover(file.getId());
        }
        if (ObjectUtil.isNotEmpty(user.getAtlas())) {
            File file = fileService.getById(user.getAtlas());
            String atlas = QiNiuConfig.CNAME + "/" + file.getFileKey();

            userVO.setAtlas(file.getId());
            userVO.setAtlasId(atlas);

        }

        if (ObjectUtil.isNotEmpty(user.getCertificate())) {
            File file = fileService.getById(user.getCertificate());
            String Certificate = QiNiuConfig.CNAME + "/" + file.getFileKey();

            userVO.setCertificate(file.getId());
            userVO.setCertificateId(Certificate);

        }


        List<TrainingExperience> trainingExperienceList = trainingMapper.selectUserId(userId);
        List<PersonalLabels> personalLabelsList = personalLabelsMapper.selectUserId(userId);
        userVO.setTrainingExperiences(trainingExperienceList);
        userVO.setPersonalLabels(personalLabelsList);
        return userVO;
    }

    /**
     * 单次时长
     *
     * @param userId
     * @return
     */
    @Override
    public DurationVO getDuration(Long userId) {

        RegularUsers regularUsers = regularUsersMapper.selectById(userId);
        //获取工作地址
        String address = regularUsers.getAddress();
        DurationVO durationVO = new DurationVO();
        if(ObjectUtil.isNotEmpty(address)){
            if (address.contains("区")) {
                int index = address.indexOf("区");
                String substring = address.substring(0, index);
                durationVO.setAddress(substring);
            }
            if (address.contains("县")) {
                int index = address.indexOf("县");
                String substring = address.substring(0, index);
                durationVO.setAddress(substring);
            }
        }

        PlanDTO planDTO = new PlanDTO();
        planDTO.setUserId(userId);
        List<PlanVO> planVOS = bookingMapper.selectPlan(planDTO);
        if (planVOS.size() > 0 && ObjectUtil.isNotEmpty(planVOS)) {
            ArrayList<BigDecimal> doubles = new ArrayList<>();
            for (PlanVO planVO : planVOS) {
                BigDecimal price = planVO.getPrice();
                doubles.add(price);
            }
            System.out.println("doubles = " + doubles);
            BigDecimal min = Collections.min(doubles);
            durationVO.setPrice(min);
        }



        return durationVO;
    }
}
